#include "Dot.h"
#include "../Utils/Math.h"
#include <QPainter>
#include "../Settings/GameSettings.h"
#include "../Utils/Math.h"

Dot::Dot(QPoint pt,unsigned int nEdge,uint nRow,uint nCol):QPoint(pt.x(),pt.y()),mnTotalEdge(nEdge),mnEdgeCount(0),mnRow(nRow),mnCol(nCol)
{
}

uint Dot::row(){return mnRow;}

uint Dot::column(){return mnCol;}

bool Dot::nearToMe(QPoint pos)
{
    return Math::distance(pos,QPoint(x(),y())) <= GameSettings::me()->setting(Game::DotRadii).value<uint>();
}

unsigned int Dot::totalEdge(){return mnTotalEdge;}

unsigned int Dot::increase()
{
    if(mnEdgeCount < mnTotalEdge){
        mnEdgeCount++;
    }

    return mnEdgeCount;
}

uint Dot::decrease()
{
    if(mnEdgeCount > 0){
        mnEdgeCount--;
    }

    return mnEdgeCount;
}

bool Dot::isSelectable()
{
    return mnEdgeCount < mnTotalEdge;
}

void Dot::draw(QPainter *pDC)
{
    unsigned int nRadius = GameSettings::me()->setting(Game::DotRadii).value<uint>();
    unsigned int nShift = nRadius / 2;

    pDC->setPen(Qt::NoPen);
    pDC->setBrush(GameSettings::me()->brush(isSelectable() ? Game::DotNormalColor : Game::DotUsedColor));
    pDC->drawEllipse(x() - nShift, y() - nShift, nRadius, nRadius);
}

QPoint Dot::point(){return QPoint(x(),y());}

void Dot::drawEllipse(QPainter *pDC, uint nRadius, QColor color)
{
    pDC->setPen(Qt::NoPen);
    pDC->setBrush(GameSettings::me()->brush(color));
    uint nShift = nRadius / 2;
    pDC->drawEllipse(x() - nShift, y() - nShift, nRadius, nRadius);
}

bool Dot::isSame(QPoint pos)
{
    return this->point() == pos;
}

bool Dot::isSame(Element *pElem, bool bByValue)
{
    Dot *pDot = dynamic_cast<Dot*>(pElem);

    if(pDot){
        if(bByValue){
            return isSame(pDot->point());
        }
        else{
            return this == pDot;
        }
    }
    else{
        return false;
    }
}

bool Dot::containerCellExist(Cell *pCell)
{
    for(int i = 0; i < mContainerCells.count(); i++){
        if(mContainerCells.at(i) == pCell){
            return true;
        }
    }

    return false;
}


bool Dot::containerLineExist(Line *pLine)
{
    for(int i = 0; i < mContainerLines.count(); i++){
        if(mContainerLines.at(i) == pLine){
            return true;
        }
    }

    return false;
}

bool Dot::addContainerCell(Cell *pCell)
{
    if(!containerCellExist(pCell)){
        mContainerCells.append(pCell);

        return true;
    }

    return false;
}

bool Dot::removeContainerCell(Cell *pCell)
{
    for(int i = 0; i < mContainerCells.count(); i++){
        if(mContainerCells.at(i) == pCell){
            mContainerCells.removeOne(pCell);
            return true;
        }
    }

    return false;
}

bool Dot::addContainerLine(Line *pLine)
{
    if(!containerLineExist(pLine)){
        mContainerLines.append(pLine);

        return true;
    }

    return false;
}

bool Dot::removeContainerLine(Line *pLine)
{
    for(int i = 0; i < mContainerLines.count(); i++){
        if(mContainerLines.at(i) == pLine){
            mContainerLines.removeOne(pLine);
            return true;
        }
    }

    return false;
}
